package com.devshephard.rpcframework.acm.tree.util;

import java.util.ArrayList;
import java.util.List;

public class BFS {

    public static void main(String[] args) {

         TreeEntity treeEntity1 = new TreeEntity();


         TreeEntity treeEntity2 = new TreeEntity();


          TreeEntity treeEntity3 = new TreeEntity();

          TreeEntity treeEntity4 = new TreeEntity();
          treeEntity4.setRootName("D");

          TreeEntity treeEntity5 = new TreeEntity();
          treeEntity5.setRootName("E");



        List<TreeEntity> children1 = new ArrayList<>();

        List<TreeEntity> children2 = new ArrayList<>();
        children2.add(treeEntity4);
        children2.add(treeEntity5);
        treeEntity2.setChildren(children2);
        treeEntity2.setRootName("B");

        treeEntity3.setChildren(new ArrayList<>());
        treeEntity3.setRootName("C");


        children1.add(treeEntity2);
        children1.add(treeEntity3);

        treeEntity1.setRootName("A");
        treeEntity1.setChildren(children1);

        List<TreeEntity> res = new ArrayList<>();

        List<TreeEntity> treeEntityListItem = new ArrayList<>();
        TreeEntity treeEntityRoot = new TreeEntity();
        treeEntityRoot.setRootName("ROOT");
        //创建树的头部
        List<TreeEntity> treeEntityListRoot = new ArrayList<>();
        treeEntityListRoot.add(treeEntity1);
        treeEntityRoot.setChildren(treeEntityListRoot);
        treeEntityListItem.add(treeEntityRoot);

        //记录树的深度，由于已经有一个头部了，因此起始深度是1
        Integer treeDeep = 0;

        while (true){

            List<TreeEntity> treeEntityListItemChild = new ArrayList<>();

            for (TreeEntity treeEntity : treeEntityListItem) {
                treeEntityListItemChild.addAll(treeEntity.getChildren());
            }

            if(treeEntityListItemChild.size() == 0){
                break;
            }else{
                treeDeep ++ ;
            }

            res.addAll(treeEntityListItemChild);

            treeEntityListItem = treeEntityListItemChild;

        }


        System.out.println(res.toString());
        System.out.println(treeDeep);

        //二叉树的层级遍历
        for (TreeEntity re : res) {
            System.out.println(re.rootName);
        }


    }



}
